<?php
include("function.php");
function h_client(){
			?>
		<form action='historique.php' method='get'>
		<input type="hidden" name="page" value="historique"/>
		<input type="hidden" name="onglet" value="client"/>
		<input type="hidden" name="search"/>
			<table>
				<tr><td>Nom :</td><td><input type="text" name="s_firstname" <?php if(isset($_GET['s_firstname'])){print "value=\"".$_GET['s_firstname']."\"";}?> /></td><td>Pr&eacute;nom :</td><td><input type="text" name="s_lastname" <?php if(isset($_GET['s_lastname'])){print "value=\"".$_GET['s_lastname']."\"";}?> /></td><td><input type="submit" value="Rechercher" /></td></tr>
			</table>
		</form>
		<?php 			
		if(isset($_GET['search'])){
			$query="Select * from clients where (`delete` <> 1) ";
			$f=array();
			if($_GET['s_firstname']!= "" || $_GET['s_lastname']!= ""){
				$query = $query."AND";
			}
			if($_GET['s_firstname']!= ""){
				$query = $query." (firstname LIKE ? ) ";
				array_push($f,$_GET['s_firstname']."%");
			}
			if($_GET['s_firstname'] != "" && $_GET['s_lastname'] != ""){
				$query = $query."AND";
			}
			if($_GET['s_lastname'] != ""){
				$query = $query." (lastname LIKE ? ) ";
				array_push($f,$_GET['s_lastname']."%");
			}
			$query = $query."order by firstname"; 
			
			echo "<div id='client' class='overflow'>";
			echo "		<p>";
			?>
			<form action='historique.php' method='get'>
			<input type="hidden" name="page" value="historique"/>
			<input type="hidden" name="onglet" value="client"/>
			<input type="hidden" name="historique"/>
			
			<?php 
			echo "			<table class='liste'>";
			foreach(db($query,$f) as $client){
				echo "				<tr><td>".$client['firstname']."</td><td>".$client['lastname']."</td><td><input type=\"radio\" name=\"id\" value=\"".$client['id']."\"></td></tr>";
			}
			echo "			</table>";
			echo "		</p>";
			echo "</div>";
			?>
			<table>
				<tr><td>Du</td><td><input type="text" size="1" MAXLENGTH="2" name="date_i_d" /> / <input type="text" size="1" MAXLENGTH="2" name="date_i_m" /> / <input type="text" size="2" MAXLENGTH="4" name="date_i_y" value="<?php print date("Y");?>"/></td><td>Au</td><td><input type="text" size="1" MAXLENGTH="2" name="date_o_d" value="<?php print date("j");?>"/> / <input type="text" size="1" MAXLENGTH="2" name="date_o_m" value="<?php print date("n");?>" /> / <input type="text" size="2" MAXLENGTH="4" name="date_o_y" value="<?php print date("Y");?>"/></td><td><input type="submit" value="Afficher l'historique" /></td></tr>
			</table>
			</form>
			<?php 
			
		}
		if(isset($_GET['historique'])){
			$client = db("select * from clients where id = ?",$_GET['id']);
			$time_in = $_GET['date_i_y']."-".$_GET['date_i_m']."-".$_GET['date_i_d']." 00:00:00";
			$time_out = $_GET['date_o_y']."-".$_GET['date_o_m']."-".$_GET['date_o_d']." 23:59:59";
			$sql ="SELECT * FROM `historique` WHERE client = (SELECT id FROM clients WHERE id = ?) AND date BETWEEN ? AND ? AND `DELETE` <> 1";
			$data = array($_GET['id'],$time_in,$time_out);
			$query="select offre.name from transaction  right join offre ON transaction.offre = offre.id WHERE transaction.id = ?";
			foreach(db($sql,$data) as $transaction){
				$date=date_modif($transaction['date']);
				print $client[0]['firstname']." ".$client[0]['lastname']." le ".$date['day']."/".$date['month']."/".$date['year']." &agrave; ".$date['hour'].":".$date['minute']." a pay&eacute; en ".$transaction['m_pay']."<br/>";
				foreach(db($query,$transaction['transaction']) as $offre){
					print $offre['name']."<br/>";
				}
			}
			
			
		}
	
}
function h_offre(){
	
?>
		<form action='historique.php' method='get'>
		<input type="hidden" name="page" value="historique"/>
		<input type="hidden" name="onglet" value="offre"/>
		<input type="hidden" name="search"/>
			<table>
				<tr><td>Nom de l'offre :</td><td><input type="text" name="s_name" /></td><td>Cat&eacute;gorie :</td><td><select name="s_categorie"><option  value="">Tout</option><option  value="forfais">Forfais</option><option  value="carte">A la carte</option><option  value="produit">Produit</option></select></td><td><input type="submit" value="Rechercher" /></td></tr>
			</table>
		</form>
		<?php 			
		if(isset($_GET['search'])){
			$query="Select * from offre where (`delete` <> 1) ";
			$f=array();
			if($_GET['s_name']!= "" || $_GET['s_categorie']!= ""){
				$query = $query."AND";
			}
			if($_GET['s_name']!= ""){
				$query = $query." (name LIKE ? ) ";
				array_push($f,$_GET['s_name']."%");
			}
			if($_GET['s_name'] != "" && $_GET['s_categorie'] != ""){
				$query = $query."AND";
			}
			if($_GET['s_categorie'] != ""){
				$query = $query." (categorie LIKE ? ) ";
				array_push($f,$_GET['s_categorie']."%");
			}
			$query = $query."order by name"; 
			
			echo "<div id='client' class='overflow'>";
			echo "		<p>";
			?>
			<form action='historique.php' method='get'>
			<input type="hidden" name="page" value="historique"/>
			<input type="hidden" name="onglet" value="offre"/>
			<input type="hidden" name="historique"/>
			
			<?php 
			echo "			<table class='liste'>";
			foreach(db($query,$f) as $offre){
				echo "				<tr><td>".$offre['name']."</td><td>".$offre['categorie']."</td><td>".$offre['price']."</td><td><input type=\"radio\" name=\"id\" value=\"".$offre['id']."\"></td></tr>";
			}
			echo "			</table>";
			echo "		</p>";
			echo "</div>";
						?>
			<table>
				<tr><td>Du</td><td><input type="text" size="1" MAXLENGTH="2" name="date_i_d" /> / <input type="text" size="1" MAXLENGTH="2" name="date_i_m" /> / <input type="text" size="2" MAXLENGTH="4" name="date_i_y" value="<?php print date("Y");?>"/></td><td>A</td><td><input type="text" name="date_i_h" size="1" MAXLENGTH="2" />:<input type="text" name="date_i_min" size="1" MAXLENGTH="2" /></td><td>Au</td><td><input type="text" size="1" MAXLENGTH="2" name="date_o_d" value="<?php print date("j");?>"/> / <input type="text" size="1" MAXLENGTH="2" name="date_o_m" value="<?php print date("n");?>" /> / <input type="text" size="2" MAXLENGTH="4" name="date_o_y" value="<?php print date("Y");?>"/></td><td>A</td><td><input type="text" name="date_o_h" size="1" MAXLENGTH="2" value="<?php print date("H");?>"/>:<input type="text" name="date_o_min" size="1" MAXLENGTH="2" value="<?php print date("i");?>" /></td><td><input type="submit" value="Afficher l'historique" /></td></tr>
				</table>
			</form>
			<?php 
		}
		if(isset($_GET['historique'])){
				print "pass";
				$offre = db("select * from offre where id = ?",$_GET['id']);
				$time_in = $_GET['date_i_y']."-".$_GET['date_i_m']."-".$_GET['date_i_d']." ".$_GET['date_i_h'].":".$_GET['date_i_min'].":00";
				$time_out = $_GET['date_o_y']."-".$_GET['date_o_m']."-".$_GET['date_o_d']." ".$_GET['date_o_h'].":".$_GET['date_o_min'].":59";
				$sql ="SELECT * FROM `transaction` WHERE offre = ?";
				$data = array($_GET['id']);
				$requete = "select clients.firstname,clients.lastname from historique right join clients ON historique.client = clients.id WHERE historique.transaction = ? AND date Between ? and ?";
				foreach(db($sql,$data) as $transaction){
					$data2 = array($transaction['id'],$time_in,$time_out);
					foreach(db($requete,$data2) as $client){
						print $client['firstname']."<br/>";
					}
				}
				
				
			
		}
	}
?>

<p><a href="?page=historique">Client</a> <a href="?page=historique&onglet=offre" >Offre</a></p>
<?php 
if(!isset($_GET['onglet'])){
		h_client();
	}else{
		if($_GET['onglet']=="client"){
			h_client();
		}elseif($_GET['onglet']=="offre"){
			h_offre();
		}
	}
?>